/*
 * SmtPlug.h
 *
 *  Created on: 2013-3-24
 *      Author: root
 */

#ifndef SMTPLUG_H_
#define SMTPLUG_H_

#include "types.h"
#include <unistd.h>
#include <arpa/inet.h>

#include <stdio.h>
#include <iostream>

#include "cmd.h"
#include "debug.h"
#include "SmtPlug.h"
#include "sqldb.h"
#include "DateTime.h"

#include "UdpSocket4Dev.h"
#include "UdpSocket4Usr.h"

class SmtPlug {
public:
	SmtPlug();
	virtual ~SmtPlug();

public:
	void InitBind();
	void InitDevInfo();
	void InitUsrInfo();

	int StartWork();
	int StartDevWork();
	int StartUsrWork();
	int StartCountDown();

	void StopWork();
	void StopUsrWork();
	void StopDevWork();
	void StopCountDown();

#if 1

	uint16 Ir2Uint16(uint8 *pSrc,uint16 srcByteCnt,uint16 *pDst);
	uint16 Uint162Ir(uint16 *pSrc,uint16 srcByteCnt,uint8 *pDst);

	int LogUsrData(struct sockaddr_in &addr,HValue_t &value);
	int LogUsrDataDefault(struct sockaddr_in &addr,HValue_t &value,char *sTable);
	int LogUsrAdminData(struct sockaddr_in &addr,HValue_t &value,char *sTable);
	int LogUsrSetPollFreq(struct sockaddr_in &addr,HValue_t &value,char *sTable);
	int LogUsrModifyPswd(struct sockaddr_in &addr,HValue_t &value,char *sTable);
	int LogUsrDataIRCode(struct sockaddr_in &addr,HValue_t &value,char *sTable);

	int LogDevData(struct sockaddr_in &addr,HValue_t &value);
	int LogDevDataIRCode(struct sockaddr_in &addr,HValue_t &value,char *sTable);

	int LogDevAllValue(struct sockaddr_in &addr,HValue_t &value,char *sTable);

	//bool CheckCmd(Cmd_t &cmd);
	int ParseDevData(struct sockaddr_in &addr,HValue_t &value);
	int ParseUsrData(struct sockaddr_in &addr,HValue_t &value);

	//int  ProccessCmd(int fd,Cmd_t &cmd,u32Value_t& value);
	int FindUsrAndPswd(Info_t &info);
	int FillCountDown(uint32 devId);

	void ProccessAdminDevAdd(struct sockaddr_in &addr,HValue_t &value);
	void ProccessAdminDevDelete(struct sockaddr_in &addr,HValue_t &value);
	void ProccessAdminUsrDelete(struct sockaddr_in &addr,HValue_t &value);
	void ProccessAdminUsrAdd(struct sockaddr_in &addr,HValue_t &value);

	void ProccessCxDevIsonline(struct sockaddr_in &addr,HValue_t &value);
	void ProccessForceDevOffline(struct sockaddr_in &addr,HValue_t &value);
	void ProccessUsrLogin(struct sockaddr_in &addr,HValue_t &value);
	void ProccessUsrModifyPswd(struct sockaddr_in &addr,HValue_t &value);
	int ProccessUsrReg(struct sockaddr_in &addr,HValue_t &value);
	int ProccessUsrBindDev(struct sockaddr_in &addr,HValue_t &value);
	int ProccessUsrUnBindDev(struct sockaddr_in &addr,HValue_t &value);
	int bytes2String(const uint8* pSrc, uint8* pDst, int nSrcLength);
	int string2Bytes(const uint8* pSrc, uint8* pDst, int nSrcLength);
	uint8 GetCount(uint8* pSrc);
/*
	bool lockTableWrite(myDatabase *pDB,string sTable);
	bool lockTableRead(myDatabase *pDB,string sTable);
	bool unlockTable(myDatabase *pDB);
	*/
#endif


	UdpSocket4Dev *m_pdSocket;
	UdpSocket4Usr *m_puSocket;

	IdPswdMap m_usrIdPswdMap;
	IdPswdMap m_devIdPswdMap;

	IdAddrMap m_usrIdAddrMap;
	IdAddrMap m_devIdAddrMap;

	IdMap m_usrdevMap;//find dev by usr  :usrid-devid
	IdMap m_devusrMap;//find usr by dev :devid-usrid


	//pthread_mutex_t m_workMutex;
	//pthread_mutexattr_t m_attr;
	//res=pthread_mutex_lock(&m_workMutex);
	//res=pthread_mutex_unlock(&m_workMutex);

private:
	pthread_t m_pthServerUsr;
	pthread_t m_pthServerDev;
	pthread_t m_pthCountDown;

};

#endif /* SMTPLUG_H_ */
